
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:dio/adapter.dart';
import 'package:flutter/foundation.dart';
class YYHttpRequest {
  static final Dio dio = Dio();
  static Future request<T>(String url,
      Function(T) onSuccess,
      Function(String error) onError, {
    Map<String, dynamic> params,
    String method = YYHttpRequestMethod.Get,
    Map<String, dynamic> headers = const {},
  }) async {
    // 1.创建单独配置
    final options = Options(method: method);

    if (headers.isNotEmpty) {
      headers.forEach((key, value) {
        YYHttpRequestHeader.headers[key] = value.toString();
      });
    }
    print(YYHttpRequestHeader.headers);
    options.headers = YYHttpRequestHeader.headers;

//    if (!kReleaseMode){
//      //设置代理 抓包用
//      (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (HttpClient client) {
//        client.findProxy = (uri) {
//          return "PROXY localhost:8888";
//        };
//      };
//    }
    // 2.发送网络请求
    try {
      Response response = await dio.request(url, queryParameters: params, options: options);
       onSuccess(response.data);
       return response.data;
    } on DioError catch(e) {
       onError(e.toString());
       return Future.error(e.toString());
    }

  }
}

extension YYHttpRequestMethod on YYHttpRequest {
  static const String Get = "get";
  static const String Post = "post";
  static const String Delet = "delete";
  static const String Put = "put";
}

extension YYHttpRequestHeader on YYHttpRequest {

   static  Map<String, dynamic> headers = {"app":"iOS","authorization":""};
}